<?php
namespace app\index\controller;
use app\index\common\Downloadimg;
use app\index\model\Tag;
use app\index\model\User;
use think\Controller;
use think\Hook;
use think\View;
use app\index\model\Blog;
use think\Db;
use app\index\common\OAuthcommon;
use app\extra\weibo\SaeTClientV2;
use app\extra\weibo\SaeTOAuthV2;
class Index extends Controller
{
    public function logout1(){
        session_start();
        session_destroy();
    }
    public function logout(){
        $status = input('post.status');
        if($status==='logout'){
            session_start();
            session_destroy();
        }
    }

    public function weibocallback(){
        session_start();
        header('Content-Type: text/html; charset=UTF-8');
        define("WB_AKEY", '3790507299');
        define("WB_SKEY", 'e5e2de2f566aa4edf9ce53c35c594fc0');
        define("WB_CALLBACK_URL", 'http://blog.mchyweb.top/thinkphp-practice/public/index/index/weibocallback.php');

        $o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );

        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                $token = $o->getAccessToken( 'code', $keys ) ;
            } catch (OAuthException $e) {
            }
        }

        if ($token) {
            $_SESSION['token'] = $token;
            setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );

            $u = new User();
            $o = new OAuthcommon();
            $user_message1 = $o->Weiboinfo();
            $username = $user_message1['idstr'];
            $result = $u->where('username',$username)->find();
            if ($result){

            }else{

                $email = $user_message1['url'];
                $password = $user_message1['screen_name'];
                $u->save([
                    'username'=>$username,
                    'email' => $email,
                    'password' => $password,
                    'status' => '1',

                ]);


            }


            $this->success('登录成功','index/index/index');

        } else {

        }

    }

    public function index()
    {



        $list = Blog::with('category','tag')->order('create_time','desc')->paginate(8);

        $view =new View();
        $view->username=session('username');
        $view->assign('list',$list);
        $top = $this->displaytop();
        $view->assign('top',$top);
        $category=Db::name('category')->where('display',1)->select();
        $view->assign('category',$category);
        //微博登录开始
        $o = new OAuthcommon();
        $code_url = $o->Weibourl();
        $view->assign('code_url',$code_url);

        //微博信息读取开始
        $user_message = $o->Weiboinfo();
        $view->assign('user_message',$user_message);
        //微博信息读取结束


        $tag = new Tag();
        $taglist = $tag->select();
        $view->assign('taglist',$taglist);
        return $view->fetch('index');

    }

    public function displaytop()
    {
        $blog = new Blog();
        $top = $blog
            ->where('is_top',1)
            ->select();
        return $top;
    }

    public function search()
    {
        $blog = new Blog();
        $search=input('search');
        $view = new View();
        $view->username=session('username');
        $result = $blog->where('blog_title','like',"%$search%")->paginate(8,false,['query' => request()->param()]);
        $view->assign('blog_title',$search);
        $view->assign('result',$result);

        $top = $this->displaytop();
        $view->assign('top',$top);

        $tag = new Tag();
        $taglist = $tag->select();
        $view->assign('taglist',$taglist);

        $category=Db::name('category')->where('display',1)->select();
        $view->assign('category',$category);

        //微博登录开始
        $o = new OAuthcommon();
        $code_url = $o->Weibourl();
        $view->assign('code_url',$code_url);

        //微博信息读取开始
        $user_message = $o->Weiboinfo();
        $view->assign('user_message',$user_message);
        //微博信息读取结束

        return $view->fetch('search');
    }

    public function tag($tid='')
    {
        $blog = new Blog();
        $view = new View();
        $tagmodel = new Tag();
        $view->username=session('username');
        $tagname = $tagmodel->where('tid',$tid)->find();
        $result = $blog->where('tid',$tid)->paginate(8,false,['query' => request()->param()]);
        $view->assign('tag',$tagname);
        $view->assign('result',$result);

        $top = $this->displaytop();
        $view->assign('top',$top);

        $tag = new Tag();
        $taglist = $tag->select();
        $view->assign('taglist',$taglist);

        $category=Db::name('category')->where('display',1)->select();
        $view->assign('category',$category);

        //微博登录开始
        $o = new OAuthcommon();
        $code_url = $o->Weibourl();
        $view->assign('code_url',$code_url);

        //微博信息读取开始
        $user_message = $o->Weiboinfo();
        $view->assign('user_message',$user_message);
        //微博信息读取结束

        return $view->fetch('tag');
    }

    public function category($cid='')
    {
        $blog = new Blog();
        $view = new View();
        $view->username=session('username');
        $result = $blog->where('cid',$cid)->paginate(8,false,['query' => request()->param()]);
        $view->assign('result',$result);

        $top = $this->displaytop();
        $view->assign('top',$top);

        $tag = new Tag();
        $taglist = $tag->select();
        $view->assign('taglist',$taglist);

        $category=Db::name('category')->where('display',1)->select();
        $view->assign('category',$category);

        //微博登录开始
        $o = new OAuthcommon();
        $code_url = $o->Weibourl();
        $view->assign('code_url',$code_url);

        //微博信息读取开始
        $user_message = $o->Weiboinfo();
        $view->assign('user_message',$user_message);
        //微博信息读取结束

        return $view->fetch('category');
    }
}